"use strict";
import axios ,{AJAX_START,AJAX_SUCCESS,AJAX_ERROR} from  "../../lib/networkAxios";
import * as type from "../mutation-types";

//--------------------
//state
//--------------------
export const state = {

};
//--------------------
//getters
//--------------------
export const getters = {
};
//--------------------
//actions
//--------------------

const registrPost=({commit},username)=>{
    commit(type.REGISTR_POST,user)
};

export const actions = {
    registrPostDispatch ({commit},data) {
        registrPost({commit},{status:AJAX_START})
        return axios.post(`${__SERVER_HOST__}/AdminApi/Register`,data).then(res=>{
            registrPost({commit},{status:AJAX_SUCCESS,info:res.data,msg:res.msg})
            return  Promise.resolve(res)
        }).catch(res=>{
            registrPost({commit},{status:AJAX_ERROR,msg:res.msg})
            return  Promise.reject(res)
        })
    }
};

//--------------------
//Mutations Handle
//--------------------

const registrPostMutationHandle =(state,action)=>{
    switch (action.status){
        case AJAX_START:
            return Object.assign({}, state, {
                isAxiosing: true,
            });
        case AJAX_SUCCESS:
            return Object.assign({}, state, {
                isAxiosing: false,
                info:action.info,
                msg:action.msg
            });
        case AJAX_ERROR:
            return Object.assign({}, state, {
                isAxiosing: false,
                msg:action.msg
            })
    }
};


//--------------------
//Mutations
//--------------------
export const mutations = {
    [type.REGISTR_POST](state,action) {
        console.log(state)
        console.log(action)
        return state= Object.assign({},state,{user:registrPostMutationHandle(state["user"],action)})
    },

};

